/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package top.dzygod.modules.pay.service.impl;

import top.dzygod.modules.pay.domain.School;
import top.dzygod.modules.pay.service.dto.SchoolSmallDto;
import top.dzygod.utils.ValidationUtil;
import top.dzygod.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import top.dzygod.modules.pay.repository.SchoolRepository;
import top.dzygod.modules.pay.service.SchoolService;
import top.dzygod.modules.pay.service.dto.SchoolDto;
import top.dzygod.modules.pay.service.dto.SchoolQueryCriteria;
import top.dzygod.modules.pay.service.mapstruct.SchoolMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import top.dzygod.utils.PageUtil;
import top.dzygod.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://dzygod.top
* @description 服务实现
* @author dzygod
* @date 2020-07-28
**/
@Service
@RequiredArgsConstructor
public class SchoolServiceImpl implements SchoolService {

    private final SchoolRepository schoolRepository;
    private final SchoolMapper schoolMapper;

    @Override
    public Map<String,Object> queryAll(SchoolQueryCriteria criteria, Pageable pageable){
        Page<School> page = schoolRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(schoolMapper::toDto));
    }

    @Override
    public List<SchoolDto> queryAll(SchoolQueryCriteria criteria){
        return schoolMapper.toDto(schoolRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public SchoolDto findById(Long schoolId) {
        School school = schoolRepository.findById(schoolId).orElseGet(School::new);
        ValidationUtil.isNull(school.getId(),"School","schoolId",schoolId);
        return schoolMapper.toDto(school);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SchoolDto create(School resources) {
        return schoolMapper.toDto(schoolRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(School resources) {
        School school = schoolRepository.findById(resources.getId()).orElseGet(School::new);
        ValidationUtil.isNull( school.getId(),"School","id",resources.getId());
        school.copy(resources);
        schoolRepository.save(school);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long schoolId : ids) {
            schoolRepository.deleteById(schoolId);
        }
    }

    @Override
    public void download(List<SchoolDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SchoolDto school : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("学校名称", school.getName());
            map.put("创建时间", school.getGmtCreate());
            map.put("修改时间", school.getGmtModified());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
